{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "6985ecdb",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-07-12T07:15:59.987681Z",
     "start_time": "2023-07-12T07:15:58.966943Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "hello\n",
      "world\n"
     ]
    }
   ],
   "source": [
    "import asyncio\n",
    "\n",
    "async def main():\n",
    "    print('hello')\n",
    "    await asyncio.sleep(1)\n",
    "    print('world')\n",
    "    \n",
    "await main()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "b43b4019",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-07-12T07:20:27.746881Z",
     "start_time": "2023-07-12T07:20:24.723278Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "started at 15:20:24\n",
      "hello\n",
      "world\n",
      "finished at 15:20:27\n"
     ]
    }
   ],
   "source": [
    "import time\n",
    "\n",
    "async def say_after(delay, what):\n",
    "    await asyncio.sleep(delay)\n",
    "    print(what)\n",
    "    \n",
    "async def main():\n",
    "    print(f\"started at {time.strftime('%X')}\")\n",
    "    \n",
    "    await say_after(1, 'hello')\n",
    "    await say_after(2, 'world')\n",
    "    \n",
    "    print(f\"finished at {time.strftime('%X')}\")\n",
    "          \n",
    "await main()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "4762d97a",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-07-12T07:22:32.611116Z",
     "start_time": "2023-07-12T07:22:32.606586Z"
    }
   },
   "outputs": [],
   "source": [
    "async def main():\n",
    "    task1 = asyncio.create_task(\n",
    "        say_after(1, 'hello'))\n",
    "\n",
    "    task2 = asyncio.create_task(\n",
    "        say_after(2, 'world'))\n",
    "\n",
    "    print(f\"started at {time.strftime('%X')}\")\n",
    "\n",
    "    # Wait until both tasks are completed (should take\n",
    "    # around 2 seconds.)\n",
    "    await task1\n",
    "    await task2\n",
    "\n",
    "    print(f\"finished at {time.strftime('%X')}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "367cb56c",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-07-12T07:22:47.888449Z",
     "start_time": "2023-07-12T07:22:45.886043Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "started at 15:22:45\n",
      "hello\n",
      "world\n",
      "finished at 15:22:47\n"
     ]
    }
   ],
   "source": [
    "await main()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "5f26767c",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-07-12T07:28:33.184280Z",
     "start_time": "2023-07-12T07:28:31.167975Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "started at 15:28:31\n",
      "hello\n",
      "world\n",
      "finished at 15:28:33\n"
     ]
    }
   ],
   "source": [
    "async def main():\n",
    "    async with asyncio.TaskGroup() as tg:\n",
    "        task1 = tg.create_task(\n",
    "        say_after(1, 'hello'))\n",
    "        \n",
    "        task2 = tg.create_task(\n",
    "        say_after(2, 'world'))\n",
    "        print(f\"started at {time.strftime('%X')}\")\n",
    "        \n",
    "    print(f\"finished at {time.strftime('%X')}\")\n",
    "    \n",
    "await main()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c4ef8d8e",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-07-12T07:33:03.603207Z",
     "start_time": "2023-07-12T07:33:03.599479Z"
    }
   },
   "source": [
    "### async创建一个协程，await来激活这个协程"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "5e0e2772",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-07-12T07:35:51.886550Z",
     "start_time": "2023-07-12T07:35:51.880246Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "42\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\11153\\AppData\\Local\\Temp\\ipykernel_3632\\1386468353.py:5: RuntimeWarning: coroutine 'nested' was never awaited\n",
      "  nested()\n",
      "RuntimeWarning: Enable tracemalloc to get the object allocation traceback\n"
     ]
    }
   ],
   "source": [
    "async def nested():\n",
    "    return 42\n",
    "\n",
    "async def main():\n",
    "    nested()\n",
    "    print(await nested())\n",
    "    \n",
    "await main()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "74b6c66b",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
